import { typeOf } from './assist'


export default function (time, format = 'yyyy-mm-dd HH:MM:SS') {
    if (!time) return '';
    const date = typeOf(time) === 'date' ? time : new Date(time),
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hours = date.getHours(),
        minutes = date.getMinutes(),
        seconds = date.getSeconds(),
        formMap = {
            'yyyy': year,
            'yy': year,
            'mm': month,
            'dd': day,
            'HH': hours,
            'MM': minutes,
            'SS': seconds
        };
    Object.entries(formMap).forEach(([key, val]) => {
        val = val > 9 ? val : '0' + parseInt(val);
        format = format.replace(key, val);
    });
    return format;
};